package com.sanno.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sanno.account.dao.AccountDao;
import com.sanno.account.entity.Account;
import com.sanno.account.entity.addList;
import com.sanno.account.entity.find1;
import com.sanno.account.service.AccountService;
import com.sanno.account.util.RedisUtils;
import com.sanno.account.vo.AccountVo;
import com.sanno.account.vo.SccDrugstoreVo;
import com.sanno.common.entity.SccDrugstore;
import com.sanno.common.entity.SccRole;
import com.sanno.common.entity.SncUserLogin;
import com.sanno.common.result.Result;
import com.sanno.common.result.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/account")
@Api(tags = "系统账号管理-系统账号列表")
@CrossOrigin
public class AccountController {

    @Resource
    private AccountService accountService;

    @Resource
    private AccountDao accountDao;

    @Resource
    private RedisUtils redisUtils;

    @RequestMapping(value = "test2",method = RequestMethod.GET)
    @ApiOperation(value = "test2",notes = "",httpMethod = "GET")
    public Result test2(){
        List<find1> list = accountDao.find1();
        for (find1 find1 : list) {
            System.out.println(find1);
        }
        List<SccDrugstore> list1 = accountDao.find2(list);
        return Result.ok(list1);
    }

    @RequestMapping(value = "test1",method = RequestMethod.GET)
    @ApiOperation(value = "test1",notes = "",httpMethod = "GET")
    public Result test1(String searchParams,int page,int limit) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        SccDrugstoreVo sccDrugstoreVo = new SccDrugstoreVo();
        if(searchParams != null){
            sccDrugstoreVo = objectMapper.readValue(searchParams,SccDrugstoreVo.class);
        }

        List<find1> list = accountDao.find1();

        PageHelper.startPage(page, limit);
        List<SccDrugstore> list1 = accountDao.find2(list);
        PageInfo<SccDrugstore> pageInfo = new PageInfo<>(list1);
        return Result.ok(list1,pageInfo.getTotal());
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ApiOperation(value = "test",notes = "",httpMethod = "GET")
    public Result test(String searchParams,int page,int limit) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        AccountVo accountVo = new AccountVo();
        if(searchParams != null){
            accountVo = objectMapper.readValue(searchParams,AccountVo.class);
        }

        List<SncUserLogin> list = accountDao.findUserLogin(accountVo.getName(),accountVo.getProvince(),accountVo.getCity(),accountVo.getCounty());
        for (SncUserLogin sncUserLogin : list) {
            System.out.println(sncUserLogin);
        }
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "querySncUserLogins",method = RequestMethod.GET)
    @ApiOperation(value = "多表条件联查",notes = "",httpMethod = "GET")
    public Result findAll(String searchParams,int page,int limit) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        AccountVo accountVo = new AccountVo();
        if(searchParams != null){
            accountVo = objectMapper.readValue(searchParams,AccountVo.class);
        }
        if(accountVo.getName() == null){accountVo.setName("%%");}else{
            String name = "%"+accountVo.getName()+"%";
            accountVo.setName(name);
        }

        PageHelper.startPage(page, limit);
        List<SncUserLogin> list = accountService.findUserLogin(accountVo.getName(),accountVo.getProvince(),accountVo.getCity(),accountVo.getCounty());
        PageInfo<SncUserLogin> pageInfo = new PageInfo<>(list);
        return Result.ok(list,pageInfo.getTotal());
    }

    @RequestMapping(value = "required",method = RequestMethod.GET)
    @ApiOperation(value = "required",notes = "",httpMethod = "GET")
    public Result required(){
        List<SccRole> list = accountDao.getRole();
        return Result.build(list,ResultCodeEnum.SUCCESS);
    }

//    @RequestMapping(value = "findAll",method = RequestMethod.GET)
//    @ApiOperation(value = "条件查询",notes = "",httpMethod = "GET")
//    public Result querySncUserLogins(String searchParams,int page,int limit) throws Exception{
//        ObjectMapper objectMapper = new ObjectMapper();
//        AccountVo accountVo = new AccountVo();
//        if(searchParams != null){
//            accountVo = objectMapper.readValue(searchParams,AccountVo.class);
//        }
//        if(accountVo.getName() == null){
//            accountVo.setName("");
//        }
//        QueryWrapper<SncUserLogin> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("name",accountVo.getName());
//        queryWrapper.like("status",1);
//        Page<SncUserLogin> newPage = new Page(page,limit);
//        Page<SncUserLogin> sncUserLoginPage = accountDao.selectPage(newPage, queryWrapper);
//        List<SncUserLogin> records = sncUserLoginPage.getRecords();
//        return Result.ok(records,sncUserLoginPage.getTotal());
//    }

    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    @ApiOperation(value = "id删除",notes = "",httpMethod = "POST")
    public Result delSncUserLogin(@PathVariable("id")int id){
        SncUserLogin sncUserLogin = new SncUserLogin();
        sncUserLogin.setStatus(0);
        UpdateWrapper<SncUserLogin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        boolean isOk = accountService.update(sncUserLogin,updateWrapper);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "delSncUserLogins",method = RequestMethod.POST)
    @ApiOperation(value = "ids删除",notes = "",httpMethod = "POST")
    public Result delSncUserLogins(String ids){
        String newIds = ids.substring(0,ids.lastIndexOf(","));
        String idArr[] = newIds.split(",");
        SncUserLogin sncUserLogin = new SncUserLogin();
        sncUserLogin.setStatus(0);
        UpdateWrapper<SncUserLogin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",idArr);
        boolean isOk = accountService.update(sncUserLogin,updateWrapper);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "addSncUserLogin",method = RequestMethod.POST)
    @ApiOperation(value = "添加",notes = "",httpMethod = "POST")
    public Result addSncUserLogin(String token,String name,String phone,Integer roleId,String storeId){
        String newIds = storeId.substring(0,storeId.lastIndexOf(","));
        String idArr[] = newIds.split(",");


        SncUserLogin sncUserLogin = new SncUserLogin();
        sncUserLogin.setToken(token);
        sncUserLogin.setName(name);
        sncUserLogin.setPhone(phone);

        int rows1 = accountDao.addUser(sncUserLogin);
        int id = sncUserLogin.getId();
        int rows2 = accountDao.addUserRole(id,roleId);
        List<addList> list = new ArrayList<>();
        for (String s : idArr) {
            addList addList = new addList();
            addList.setId(id);
            addList.setStoreId(Integer.parseInt(s));
            list.add(addList);
        }
        int rows3 = accountDao.addUserStore(list);
        return Result.build(rows3,ResultCodeEnum.SUCCESS);
    }

    @RequestMapping(value = "updateSncUserLogin",method = RequestMethod.POST)
    @ApiOperation(value = "修改",notes = "",httpMethod = "POST")
    public Result updateSncUserLogin(SncUserLogin sncUserLogin){

        boolean isOk = accountService.updateById(sncUserLogin);
        return Result.build(isOk,ResultCodeEnum.SUCCESS);
    }



}
